package com.briup.bookstore.service.impl;

import com.briup.bookstore.common.security.SecurityContext;
import com.briup.bookstore.domain.bean.Book;
import com.briup.bookstore.domain.vo.BookVO;
import com.briup.bookstore.mapper.CollectMapper;
import com.briup.bookstore.service.CollectService;
import com.briup.bookstore.utils.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CollectServiceImpl implements CollectService {

    private final CollectMapper collectMapper;

    @Override
    public List<BookVO> queryAllCollect() {
        // 获取当前登录用户id
        Integer userId = SecurityContext.userId();
        // 查询数据
        List<Book> books = collectMapper.queryAllCollect(userId);
        // 转换为VO
        return BeanUtil.convertList(books, BookVO.class);
    }

    @Override
    public void addCollect(Integer bookId) {
        // 获取当前登录用户的id
        Integer userId = SecurityContext.userId();
        // 调用mapper层方法，添加收藏记录
        collectMapper.addCollect(userId, bookId);
    }

    @Override
    public void cancelCollect(List<Integer> ids) {
        // 获取当前登录用户的id
        Integer userId = SecurityContext.userId();
        // 调用mapper层方法，删除收藏记录
        collectMapper.cancelCollect(userId, ids);
    }
}
